package com.woniu.cloud.movie.movie3assembly.controller;


import com.woniu.cloud.movie.api.MovieApi;
import com.woniu.cloud.movie.api.dto.MovieDto;
import com.woniu.cloud.movie.api.dto.MoviesDto;
import com.woniu.cloud.movie.api.dto.Result;
import com.woniu.cloud.movie.api.exception.mv.MovieException;
import com.woniu.cloud.movie.api.param.movieParam.MovieAddParam;
import com.woniu.cloud.movie.api.param.movieParam.MovieChangeParam;
import com.woniu.cloud.movie.api.param.movieParam.MovieDeleteParam;
import com.woniu.cloud.movie.api.param.movieParam.MovieLikeGetsParam;
import com.woniu.cloud.movie.movie3assembly.entity.Movie;
import com.woniu.cloud.movie.movie3assembly.service.MovieService;
import com.woniu.cloud.movie.movie3assembly.util.ComUtil;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RestController;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author tiangle
 * @since 2022-01-11
 */
@RestController
public class MovieController implements MovieApi {

    @Autowired
    private MovieService movieService;

    @ApiOperation("添加movie")
    public Result addMovie(@ApiParam(value="添加电影的封装",required = true) MovieAddParam movieAddParam) throws MovieException {
        MovieDto movieDto = movieService.saveMovie(movieAddParam);
        return Result.getInstance(movieDto).setMessage("添加成功");
    }

    @ApiImplicitParam(name = "id", value = "电影ID", dataType = "int",
            paramType = "path", required = true)
    @ApiOperation("上架movie")
    public Result upMovie(@ApiParam(value="上架电影id",required = true)Integer id) throws MovieException {
        MovieDto movieDto = movieService.upMovie(id);
        return Result.getInstance(movieDto).setMessage("上架成功");
    }


    @ApiImplicitParam(name = "id", value = "电影ID", dataType = "int",
            paramType = "path", required = true)
    @ApiOperation("下架movie")
    public Result downMovie(@ApiParam(value="下架电影id",required = true) Integer id) throws MovieException {
        MovieDto movieDto = movieService.downMovie(id);
        return Result.getInstance(movieDto).setMessage("下架成功");
    }

    @ApiOperation("模糊查找movie")
    public Result likeMovie(@ApiParam(value="模糊查找电影的信息",required = true) MovieLikeGetsParam movieLikeGetsParam) throws MovieException {
        MoviesDto moviesDto = movieService.likeMovie(movieLikeGetsParam);
        return Result.getInstance(moviesDto).setMessage("查找成功");
    }
    @ApiImplicitParam(name = "id", value = "电影ID", dataType = "int",
            paramType = "path", required = true)
    @ApiOperation("删除单部电影movie")
    public Result deleteMovie(@ApiParam(value = "电影id",required = true) Integer id) throws MovieException {
        boolean b = movieService.deleteMovie(id);
        if (b)
            return  Result.getInstance(null).setMessage("删除成功");
        throw new MovieException("删除失败");
    }

    @ApiOperation("删除多部电影movie")
    public Result deleteMovies(@ApiParam(value = "电影id",required = true) MovieDeleteParam dele) throws MovieException {
        boolean b = movieService.deleteMovies(dele);
        if (b)
            return  Result.getInstance(null).setMessage("删除成功");
        throw new MovieException("删除失败");
    }
    @ApiImplicitParam(name = "id", value = "电影ID", dataType = "int",
            paramType = "path", required = true)
    @ApiOperation("获得单部电影movie")
    public Result getMovie(@ApiParam(value = "电影id",required = true) Integer id) throws MovieException {
        Movie b = movieService.getById(id);

        if (b!=null)
            return  Result.getInstance(ComUtil.copyProperties(b,MovieDto.class)).setMessage("查找成功");
        throw new MovieException("查找失败");
    }

    @ApiOperation("更改movie")
    public Result changeMovie(@ApiParam(value="修改电影的封装",required = true) MovieChangeParam movieChangeParam) throws MovieException {
        boolean b = movieService.updateMovie(movieChangeParam);
        if (b)
            return Result.getInstance(null).setMessage("修改成功");
        throw new MovieException("修改失败");
    }
}

